﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime;

public class CTFGameManager : MonoBehaviour {

    private static CTFGameManager _instance;

    public static CTFGameManager Instance {
        get{
            return _instance;
        }
    }

    private List<BehaviorTree> flagNotTakenBehaviorTree = new List<BehaviorTree>();
    private List<BehaviorTree> flagTakenBehaviorTree = new List<BehaviorTree>();

    void Awake() {
        _instance = this;
    }

	// Use this for initialization
	void Start () {
        BehaviorTree[] tree = FindObjectsOfType<BehaviorTree>();
        foreach (var bt in tree)
        {
            if (bt.Group == 1)
            {
                flagNotTakenBehaviorTree.Add(bt);
            }
            else {
                flagTakenBehaviorTree.Add(bt);
            }
        }
	}

    public void TakeFlag() {
        foreach (var behaviorTree in flagNotTakenBehaviorTree)
        {
            if (BehaviorManager.instance.IsBehaviorEnabled(behaviorTree)) //判断行为树是否启用
            {
                behaviorTree.DisableBehavior();//禁用自身
            }
        }

        foreach (var behaviorTree in flagTakenBehaviorTree)
        {
            if (BehaviorManager.instance.IsBehaviorEnabled(behaviorTree) == false)//判断行为树是否启用
            {
                behaviorTree.EnableBehavior();//启用自身
            }
        }
    }

    public void DropFlag() {
        foreach (var behaviorTree in flagNotTakenBehaviorTree)
        {
            if (BehaviorManager.instance.IsBehaviorEnabled(behaviorTree) == false)//判断行为树是否启用
            {
                behaviorTree.EnableBehavior();//启用自身
            }
        }

        foreach (var behaviorTree in flagTakenBehaviorTree)
        {
            if (BehaviorManager.instance.IsBehaviorEnabled(behaviorTree))//判断行为树是否启用
            {
                behaviorTree.DisableBehavior();//禁用自身
            }
        }
    }
}
